package xgenerator.dao.impl;

import java.io.File;
import java.util.Properties;
import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import xgenerator.dao.DataSourceConfigDao;
import xgenerator.jdbc.JdbcUtils;
import xgenerator.util.XmlUtils;

public class DataSourceConfigDaoXmlImpl implements DataSourceConfigDao {
	
	/**
	 * for test
	 * @param args
	 */
	public static void main(String[] args) {
		DataSourceConfigDao datasourceDao = new DataSourceConfigDaoXmlImpl();
		
		Properties properties = datasourceDao.findByName("EAP_DS");
		System.out.println(properties.getProperty("name"));
		System.out.println(properties.getProperty("jdbc.driverClassName"));
		System.out.println(properties.getProperty("jdbc.url"));
		System.out.println(properties.getProperty("jdbc.username"));
		System.out.println(properties.getProperty("jdbc.password"));
	}
	
	/**
	 * 默认的数据库配置文件
	 */
	private static final String DEFAULT_CONFIG_FILE = System.getProperty("user.dir") + File.separator + "config" + File.separator + "jdbc.cfg.xml";
	
	/**
	 * 文档对象
	 */
	private Document doc;
	
	/**
	 * constructor
	 */
	public DataSourceConfigDaoXmlImpl() {
		this.doc = XmlUtils.getDocument(DEFAULT_CONFIG_FILE);
	}

	/**
	 * <p>
	 * Title：根据数据源名称，获取数据库类型
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param dataSourceName
	 * @return
	 * @see xgenerator.dao.DataSourceConfigDao#getDbType(java.lang.String)
	 */
	public String getDbType(String dataSourceName) {
		String driverClassName = XmlUtils.evaluateString("//datasource[@name=\"" + dataSourceName + "\"]/driverClassName/text()", this.doc);
		return JdbcUtils.analysisDbType(driverClassName);
	}
	
	/**
	 * <p>
	 * Title：获取配置的所有数据源列表
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 * @see xgenerator.dao.DataSourceConfigDao#findAllDataSourceNames()
	 */
	public Vector<String> findAllDataSourceNames() {
		Vector<String> dataSourceNames = new Vector<String>();
		
		NodeList nodeList = this.doc.getElementsByTagName("datasource");  //所有nodeName节点的列表
		if(null == nodeList) {
			return dataSourceNames;
		}
		for(int i=0; i<nodeList.getLength(); i++) {
			Node oneNode = nodeList.item(i);
			NamedNodeMap nodeAttrMap = oneNode.getAttributes();  //节点nodeName的所有属性的map
			Node attr = nodeAttrMap.getNamedItem("name");  //一个节点属性相当于节点的一个子节点
			dataSourceNames.add(attr.getNodeValue());
		}
		
		return dataSourceNames;
	}
	

	/**
	 * <p>
	 * Title：根据数据源名称，获取数据源配置信息
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param datasourceName
	 * @return
	 * @see xgenerator.dao.DataSourceConfigDao#findByName(java.lang.String)
	 */
	public Properties findByName(String dataSourceName) {
		Properties properties = new Properties();
		
		String driverClassName = XmlUtils.evaluateString("//datasource[@name=\"" + dataSourceName + "\"]/driverClassName/text()", this.doc);
		String url = XmlUtils.evaluateString("//datasource[@name=\"" + dataSourceName + "\"]/url/text()", this.doc);
		String username = XmlUtils.evaluateString("//datasource[@name=\"" + dataSourceName + "\"]/username/text()", this.doc);
		String password = XmlUtils.evaluateString("//datasource[@name=\"" + dataSourceName + "\"]/password/text()", this.doc);

		properties.setProperty("name", dataSourceName);
		properties.setProperty("jdbc.driverClassName", driverClassName);
		properties.setProperty("jdbc.url", url);
		properties.setProperty("jdbc.username", username);
		properties.setProperty("jdbc.password", password);
		
		return properties;
	}

	/**
	 * <p>
	 * Title：添加数据源配置信息
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param properties
	 * @return
	 * @see xgenerator.dao.DataSourceConfigDao#insert(java.util.Properties)
	 */
	public long insert(Properties properties) {
		return 0;
	}

	/**
	 * <p>
	 * Title：更新数据源配置信息
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param properties
	 * @see xgenerator.dao.DataSourceConfigDao#update(java.util.Properties)
	 */
	public void update(Properties properties) {

	}
	
	/**
	 * <p>
	 * Title：删除数据源配置信息
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param dataSourceName
	 * @see xgenerator.dao.DataSourceConfigDao#delete(java.lang.String)
	 */
	public void delete(String dataSourceName) {
		
	}

}
